<!-- 车辆列表 -->
<template>
  <div class="dashboard-container vehicle customer-list-box">
    <el-card
      class="search-card"
      shadow="never"
    >
      <el-form
        ref="searchForm"
        :inline="true"
        :model="params"
        label-width="80px"
      >
        <el-form-item
          label="车辆类型"
          prop="truckTypeId"
        >
          <el-select
            v-model="params.truckTypeId"
            placeholder="请选择车辆类型"
          >
            <el-option
              v-for="ele in carModelsList"
              :key="ele.id"
              :label="ele.name"
              :value="ele.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="车辆号码"
          prop="licensePlate"
        >
          <el-input
            v-model="params.licensePlate"
            placeholder="请输入车辆号码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="getVehicleList()"
          >提交</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <TabChange
      class="tabclass"
      :default-active-index="2"
      :setting-list="settingList"
      @tabChange="tabChange"
    ></TabChange>
    <el-card
      class="box-card"
      shadow="never"
    >
      <el-button
        type="primary"
        class="add-btn"
        @click="dialogVisible = true"
      >新增车辆</el-button>
      <el-table
        class="waybill-table"
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          type="index"
          label="序号"
          width="80"
        >
        </el-table-column>
        <el-table-column
          prop="licensePlate"
          label="车辆号码"
        >
        </el-table-column>
        <el-table-column
          prop="truckTypeName"
          label="车辆类型"
        >
        </el-table-column>
        <el-table-column
          prop="driverNum"
          label="司机数量"
        >
        </el-table-column>
        <el-table-column
          prop="workStatus"
          label="车辆状态"
          :formatter="workStatusFormatter"
        >
          <template slot-scope="scope">
            <div style="display: flex; justify-content: left; align-items: center;">
              <span
                v-show="scope.row.workStatus == 0"
                slot="reference"
                style=" margin-right: 4px;"
              >
                <i
                  class="dotClass"
                  style="background-color: #E15536"
                ></i>
              </span>
              <span
                v-show="scope.row.workStatus == 1"
                slot="reference"
                style=" margin-right: 4px;"
              >
                <i
                  class="dotClass"
                  style="background-color: #1dc779"
                ></i>
              </span>
              {{ workStatusFormatter(scope.row) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="deviceGpsId"
          label="GPS设备ID"
        >
        </el-table-column>
        <el-table-column
          prop="allowableLoad"
          label="实载重量（T）"
        >
        </el-table-column>
        <el-table-column
          prop="allowableVolume"
          label="实载体积（方）"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          width="170"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              class="edit"
              type="text"
              size="small"
              @click="getDetail(scope.row.id)"
            >查看详情</el-button>
            <span class="Partingline">|</span>
            <el-button
              :class="{ edit: true, editColor: scope.row.workStatus === 1 }"
              class="edit"
              type="text"
              size="small"
              @click="enableVehicle(scope.row)"
            >{{ scope.row.workStatus ===0 ? '启用' : '停用' }}</el-button>
            <span class="Partingline">|</span>
            <el-button
              class="edit"
              type="text"
              size="small"
              @click="assignedDriver(scope.row)"
            >配置司机</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="params.page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      v-if="dialogVisible"
      title="新增车辆"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <VehicleAdd
        :get-working-car-list="getWorkingCarList"
        :car-models-list="carModelsList"
        :get-vehicle-list="getVehicleList"
        @changeDialogVisible="changeDialogVisible"
      ></VehicleAdd>
    </el-dialog>
    <ArrangeDriver
      :title-info="titleInfo"
      :is-show-dialog="isShowDialog"
      :car-modal-info="carModalInfo"
      :drivers-list="driversList"
      @changeIsShowDialog="changeIsShowDialog"
    ></ArrangeDriver>
  </div>
</template>
<script>
import TabChange from '@/components/TabChange'
import VehicleAdd from './components/vehicle-add.vue'
import ArrangeDriver from './components/arrange-driver.vue'
import { getVehicle, getCarModelsInfo, getCountCar, getDriverList, disableVehicle, enableVehicle } from '@/api/vehicle'

export default {
  name: 'Vehicle',
  components: {
    TabChange,
    VehicleAdd,
    ArrangeDriver
  },
  data() {
    return {
      id: null,
      titleInfo: {
        text: '配置司机'
      },
      isShowDialog: false,
      dialogVisible: false,
      driversList: [],
      tableData: [],
      total: 0,
      params: {
        page: 1,
        pageSize: 10,
        truckTypeId: '', // 车辆类型ID
        workStatus: '', // 状态
        licensePlate: '' // 车辆号码
      },
      carModelsList: [],
      carModalInfo: {},
      settingList: [
        {
          value: 2,
          label: '全部',
          num: 0
        },
        {
          value: 1,
          label: '可用',
          num: 0
        },
        {
          value: 0,
          label: '停用',
          num: 0
        }
      ]
    }
  },
  created() {
    this.getVehicleList()
    this.getCarModelInfoList()
    this.getWorkingCarList()
    this.getdriversList()
  },
  methods: {
    enableVehicle(row) {
      const that = this
      const { workStatus, id } = row
      const h = this.$createElement
      this.$msgbox({
        title: `${workStatus ? '确定要停用吗？' : '确定要启用吗？'}`,
        message: h('p', null, [
          h('p', null, `车辆${workStatus ? '停用' : '启用'}需要满足以下条件：`),
          h('span', { style: 'margin-right: 32px' }, `${workStatus ? '1、车辆当前无运输任务' : '1、车辆信息已完善'}`),
          h('span', null, `${workStatus ? '2、车辆无关联运输线路' : '2、绑定司机>=2,且有排班'}`),
          h('p', { style: 'background: #FFF3EF' }, `${workStatus ? '注：停用后司机将自动解除绑定' : ''}`)
        ]),
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async(action) => {
        if (workStatus) {
          const res = await disableVehicle(id)
          if (res.code === 200) {
            that.$message.success('停用成功')
            await that.getVehicleList()
          } else {
            that.$message.error(res.msg)
          }
        } else {
          const enableRes = await enableVehicle(id)
          if (enableRes.code === 200) {
            that.$message.success('启用成功')
            await that.getVehicleList()
          } else {
            that.$message.error(enableRes.msg)
          }
        }
      }).catch(() => { })
    },
    async getdriversList() {
      const res = await getDriverList()
      this.driversList = res.data
    },
    async changeIsShowDialog(val) {
      this.isShowDialog = val
      await this.getVehicleList()
    },
    async getCarModelInfoList() {
      const res = await getCarModelsInfo()
      this.carModelsList = res.data
    },
    workStatusFormatter(row) {
      const MAP = {
        0: '停用',
        1: '启用'
      }
      return MAP[row.workStatus]
    },
    // 状态变更
    async tabChange(activeIndex) {
      if (activeIndex === 2) {
        this.params.workStatus = null
        this.params.page = 1
        this.getVehicleList()
      } else {
        this.params.page = 1
        this.params.workStatus = activeIndex
        this.getVehicleList()
      }
    },
    async getWorkingCarList() {
      const workingRes = await getCountCar()
      this.settingList[0].num = +workingRes.data[0] + +workingRes.data[1]
      this.settingList[1].num = workingRes.data[1]
      this.settingList[2].num = workingRes.data[0]
    },
    changeDialogVisible(value) {
      this.dialogVisible = value
    },
    getDetail(id) {
      this.$router.push(`vehicle-detail?id=${id}`)
    },
    async assignedDriver(row) {
      this.carModalInfo = row
      this.$confirm('<p>配置司机需要满足以下条件</p><p><span>1、车辆信息已完善</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span>2、车辆无未完成运输任务</span></p>', '车辆配置', {
        confirmButtonText: '确定',
        dangerouslyUseHTMLString: true,
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.isShowDialog = true
      }).catch(() => {})
    },
    handleClose(done) {
      done()
    },
    async resetForm() {
      await this.$refs.searchForm.resetFields()
      this.getVehicleList()
    },
    handleSizeChange(val) {
      this.params.pageSize = val
      this.getVehicleList()
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.getVehicleList()
    },
    async getVehicleList() {
      const res = await getVehicle(this.params)
      this.tableData = res.data.items
      this.total = Number(res.data.counts)
    }
  }

}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.editColor{
  color: #E15536 !important;
}
.dotClass {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: block;
}
.tabclass{
  margin:  20px 20px;
}
.el-pagination{
  padding: 20px 0;
  position: relative;
  left: 20%;
}
.search-card{
  margin: 8px 20px;
  padding: 20px 30px 0 30px;
  ::v-deep .el-card__body{
    padding: 0;
    .el-form-item__label{
      font-weight: 400;
    }
    .el-input__inner{
        width: 240px;
      }
  }
}
.cell{
    text-align: left;
    .edit{
      color: #419EFF;
    }
    .delete{
      color: #F56C6C;
    }
    .Partingline{
      color: #E3E3E3 ;
    }
}
.add-btn{
  margin-bottom: 20px;
}
.box-card{
  margin:  20px;
  padding: 30px;
  ::v-deep .el-card__body{
    padding: 0;
  }
}

.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 10px;
}
.vehicle ::v-deep .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}

.vehicle {
  ::v-deep .el-dialog__title {
    width: 73px;
    height: 25px;
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #20232a;
    line-height: 25px;
    letter-spacing: 0px;
  }
  ::v-deep .el-dialog__body {
    text-align: center;
    padding: 20px;
  }
  .warn-img {
    width: 40px;
  }
  p {
    height: 24px;
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    color: #818693;
    line-height: 24px;
  }
}

// 下拉框
::v-deep .el-select {
  width: 100%;
}
</style>

<style>
  .startconfirm{
    width: 481px;

    }
    .startconfirm .el-message-box__header{
      padding-bottom: 4px;
    }
    .startconfirm .el-message-box__container{
      margin-bottom: 15px;
    }
    .confirm .el-message-box__status.el-icon-warning{
      left: 70px;
    }
    .confirm.el-message-box__title{
      font-size: 16px;
    }
    .confirm .el-message-box__message p {
      color: #F92D2D;
      margin-top: 5px;
    }
    .confirm .el-message-box__btns{
      text-align: center;
      margin-top: 10px;
    }
</style>
